package com.wink_172.library.utils

import android.content.Context
import android.view.View
import android.view.ViewGroup

/**
 * Created by hcc on 16/8/4 21:18
 * 100332338@qq.com
 *
 *
 * 屏幕像素转换工具类
 */
object DisplayUtil {   @JvmStatic
    fun px2dp(context: Context?, pxValue: Float): Int {
        val scale = context?.resources?.displayMetrics?.density
        return (pxValue / scale!! + 0.5f).toInt()
    }

    @JvmStatic
    fun dp2px(context: Context?, dipValue: Float): Int {
        val scale = context?.resources?.displayMetrics?.density
        return (dipValue * scale!! + 0.5f).toInt()
    }
    @JvmStatic
    fun px2sp(context: Context?, pxValue: Float): Int {
        val fontScale = context?.resources?.displayMetrics?.scaledDensity
        return (pxValue / fontScale!! + 0.5f).toInt()
    }
    @JvmStatic
    fun sp2px(context: Context?, spValue: Float): Int {
        val fontScale = context?.resources?.displayMetrics?.scaledDensity
        return (spValue * fontScale!! + 0.5f).toInt()
    }
    @JvmStatic
    fun getScreenWidth(context: Context?): Int {
        val dm = context!!.resources.displayMetrics
        return dm.widthPixels
    }
    @JvmStatic
    fun getScreenHeight(context: Context?): Int {
        val dm = context!!.resources.displayMetrics
        return dm.heightPixels
    }
    @JvmStatic
    fun getDisplayDensity(context: Context?): Float {
        return context?.resources?.displayMetrics?.density ?: (-1).toFloat()
    }

}